export type MessageRole = "user" | "assistant" | "system" | "tool";

export interface ConversationInterface {
    id: string;
    title: string;
    createdAt: number;
    updatedAt: number;
    messages: Message[];
}

export type Conversations = ConversationInterface[];

export type ChatState = {
    currentConversationId: string;
    conversations: Conversations;
    currentInput: string;
    isLoading: boolean;
    isHydrated: boolean;
};

export type ChatActions = {
    initialize: (force?: boolean) => void;
    setCurrentConversationId: (conversationId: string) => void;
    sendMessage: (content: string, role?: MessageRole) => Promise<void>;
    newConversation: () => void;
    clearConversations: () => void;
    getCurrentConversation(): ConversationInterface | undefined;
    deleteConversation: (conversationId: string) => void;
    setCurrentInput: (input: string) => void;
    setIsLoading: (isLoading: boolean) => void;
};

export type ChatInterface = ChatState & ChatActions;

export type FunctionCall = {
    function: {
        arguments: { [key: string]: string };
        name: string;
    };
};

export type FunctionCallResult = {
    content: { type: "text"; text: string }[];
    isError: boolean;
};
export interface Message {
    id: string;
    role: MessageRole;
    content: string;
    timestamp: number;
    images?: string[];
    tool_calls?: Array<FunctionCall>;
    tool_results?: Array<FunctionCallResult>;
}

export interface ChatResponse {
    // Core message fields (kept for backward compatibility)
    id: string;
    content: string;
    role: MessageRole;

    // API response metadata
    model?: string;
    createdAt?: string;
    done?: boolean;
    doneReason?: string;
    context?: number[];

    // Statistics
    totalDuration?: number;
    loadDuration?: number;
    promptEvalCount?: number;
    promptEvalDuration?: number;
    evalCount?: number;
    evalDuration?: number;

    // Streaming response support
    message?: Message;

    // Error handling
    error?: string;

    // Deprecated fields (for backward compatibility)
    response?: string; // deprecated in favor of message.content
}
